package org.projectback.controller;


import jakarta.annotation.Resource;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;

import org.projectback.entity.AccountEntity.viewObject.RegisterVO;
import org.projectback.entity.AccountEntity.viewObject.ResetVO;
import org.projectback.service.AccountService.AccountService;
import org.projectback.utils.Jwt.JwtProperties;
import org.projectback.utils.Jwt.JwtUtils;
import org.projectback.utils.ResultMessage;
import org.projectback.utils.Utils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Validated
@RestController()
public class MainController {

    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private Utils utils;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private AccountService accountService;
    @Resource
    private ChatClient chatClient;

    @GetMapping("/api/hello")
    public String hello() {
        return ResultMessage.success(200, null).toJsonString();
    }

    @GetMapping("/api/user/ChatClient")
    public String generate(@RequestParam("message") String message) {
        System.out.println("正在处理...");
        String content = chatClient.prompt(message).call().content();
        System.out.println("处理完成!");
        return ResultMessage.success(200, content).toJsonString();
    }

    /**
     * 这里的接口需要参数验证 否则会出现 邮件发送异常
     *
     * @param email 邮件地址
     * @param type  发送类型
     * @return 成功200或失败402
     */
    @GetMapping("/api/user/customEmail")
    public ResultMessage<String> emailSend(@RequestParam @Email String email, @RequestParam @Pattern(regexp = "(register|reset)") String type) {
        String message = accountService.emailQueueSender(email, type, stringRedisTemplate);
        if (message == null) {
            return ResultMessage.success(200, "邮件发送成功！");
        }
        return ResultMessage.failure(402, message);
    }

    /**
     * @param registerVO 注册用户实体类
     * @return 200 or 402
     */
    @PostMapping("/api/user/register")
    public ResultMessage<Void> register(@RequestBody @Validated RegisterVO registerVO) {
        String message = accountService.registerAccount(registerVO, stringRedisTemplate);
        return message == null ? ResultMessage.success(200, null) : ResultMessage.failure(402, message);
    }

    /**
     * 重置密码邮箱验证接口
     *
     * @param email 验证邮箱
     * @return success or failure
     */
    @GetMapping("/api/user/confirmEmail")
    public ResultMessage<String> confirm(@RequestParam @Email String email) {
        String message = accountService.confirmAccount(email);
        //非空发送邮件 空返回用户不存在
        if (message == null) {
            return emailSend(email, "reset");
        } else {
            return ResultMessage.failure(402, message);
        }
    }

    @PostMapping("/api/user/reset")
    public ResultMessage<Void> reset(@RequestBody @Validated ResetVO resetVO) {
        String message = accountService.resetAccount(resetVO, stringRedisTemplate);
        return message == null ? ResultMessage.success(200, null) : ResultMessage.failure(402, message);
    }

    /**
     * 这个方法用于返回新令牌
     *
     * @param refreshToken 验证令牌
     * @return 返回新AccessToken
     */
    @GetMapping("/api/test/refresh")
    public String refresh(@RequestParam("refreshToken") String refreshToken) {
        return jwtUtils.rotateRefreshToken(stringRedisTemplate, utils, jwtProperties, refreshToken);
    }
}
